//
//  CSYRetweetedView.swift
//  CSYMicroBlockSina
//
//  Created by 姚彦兆 on 15/11/15.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit
import SnapKit
import FFLabel
class CSYRetweetedView: UIView ,FFLabelDelegate {

    private var bottomConstraint : Constraint?
    //数据源方法
    var retweetedModel : CSYMyMicroBlogModel? {
     
        didSet{
            
            contentLabel.attributedText = CSYEmoticonManager.manager.setTextToImageText(retweetedModel?.text ?? "" , font: contentLabel.font)
        
            self.bottomConstraint?.uninstall()
        
            if let urlArray : [NSURL] = retweetedModel?.picURLArray where urlArray.count > 0 {
            
                pictureView.hidden = false
                
                pictureView.imageURLArray = urlArray
                
                pictureView.backgroundColor = UIColor.whiteColor()
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
                    
                })
                
            
            }else{
                
                pictureView.hidden = true
            
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).offset(cellMargin).constraint
                    
                })
            
            
            }
            
            
            
        }
        
    }
    
    //初始化方法
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        
        setUpUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置UI
    
    private func setUpUI(){
    
        addSubview(contentLabel)
        
        contentLabel.labelDelegate = self
        
        contentLabel.linkTextColor = UIColor.orangeColor()
        
        addSubview(pictureView)
        
        pictureView.backgroundColor = UIColor.lightGrayColor()
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(snp_top).offset(cellMargin)
            
            make.left.equalTo(snp_left).offset(cellMargin)
            
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(cellMargin)
            
            make.left.equalTo(snp_left).offset(cellMargin)
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
            
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(cellMargin).constraint
            
        }
    
    }
    
    //懒加载所有控件
    
    lazy var contentLabel : FFLabel = FFLabel(textInfo: "这是转发的微博正文", fontSize: 14, infoColor: UIColor.darkGrayColor(), numberOFLines: 0, alphaValue: 1, margin: cellMargin)
    
    lazy var pictureView : CSYBlogPictureView = CSYBlogPictureView()
    

}

extension CSYRetweetedView {
    
    func labelDidSelectedLinkText(label: FFLabel, text: String) {
        
        print(text)
        
        if let URL : NSURL = NSURL(string: text){
        
            if text.hasPrefix("http"){
        
                let webVC : CSYShowLinkViewController = CSYShowLinkViewController(url: URL, blogModel: retweetedModel!)
            
                self.getNavController()?.pushViewController(webVC, animated: true)
                
            }
            
        }
        
    }
    
}
